package com.yufubao.service.service.common.impl;

import com.yufubao.framework.builder.es.RequestOptionsBuilder;
import com.yufubao.framework.constant.EsConstant;
import com.yufubao.framework.enums.CommonExceptionEnum;
import com.yufubao.framework.exception.BaseException;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.service.service.common.ElasticsearchUtilService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.TimeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.yufubao.framework.constant.EsConstant.TIME_VALUE_SECONDS;
import static com.yufubao.framework.enums.CommonExceptionEnum.ERROR500;
import static com.yufubao.framework.enums.CommonExceptionEnum.ES_ERROR;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 通用es工具
 * @date 2024/9/6 17:33
 */
@Slf4j
@Service("elasticsearchUtilService")
public class ElasticsearchUtilServiceImpl implements ElasticsearchUtilService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * @description 单个删除商户
     * @param supplierInfoId
     * @return void
     * @author zhangyiheng
     * @date 2024/9/6 17:37
     */
    @Override
    public void supplierInfoDel(Long supplierInfoId) {
        try {
            DeleteRequest deleteRequest = new DeleteRequest();
            deleteRequest.index(EsConstant.SUPPLIER_INDEX);
            deleteRequest.id(String.valueOf(supplierInfoId));
            restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error(ES_ERROR.getMsg(), e);
            throw new BaseException(ERROR500);
        }
    }

    /**
     * @param supplierInfoId
     * @param parameters     参数
     * @param filed          字段名
     * @return com.yufubao.framework.result.ResponseResult
     * @description 单个修改商户
     * @author zhangyiheng
     * @date 2024/9/6 17:35
     */
    @Override
    public ResponseResult updateEsRst(Long supplierInfoId, String parameters, String filed) {
        try {
            UpdateRequest updateRequest = new UpdateRequest(EsConstant.SUPPLIER_INDEX, supplierInfoId.toString());
            Map<String, Object> kvs = new HashMap<>();
            kvs.put(filed, parameters);
            updateRequest.doc(kvs);
            updateRequest.timeout(TimeValue.timeValueSeconds(TIME_VALUE_SECONDS));
            updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
            restHighLevelClient.update(updateRequest, RequestOptionsBuilder.COMMON_OPTIONS);
            return ResponseResult.ok();
        } catch (IOException e) {
            log.error(ES_ERROR.getMsg(), e);
            throw new BaseException(CommonExceptionEnum.ERROR);
        }
    }
}
